package com.hxx.day1.service.impl;

import com.hxx.day1.domain.Person;
import com.hxx.day1.service.PersonService;

public class PersonServiceImpl implements PersonService {

    private Person[] db = new Person[3];

    @Override
    public void save(Person person) {
        for (int index = 0; index < db.length; index++) {
            if (db[index] == null){
                db[index] = person;
                return;
            }
        }
       System.out.println("数据库已满");
    }

    @Override
    public void delete(Person person) {
        int index = find(person);
        if (index != -1){
            db[index] = null;
            System.out.println("删除成功");
        }else {
            System.out.println("数据库中没有该数据");
        }
    }

    @Override
    public void update(Person person) {
        int index = find(person);
        if (index != -1){
            Person p = db[index];
            p.setAge(person.getAge());
            p.setSex(person.getSex());
            p.setUserName(person.getUserName());
            System.out.println("修改成功");
        }else {
            System.out.println("数据库中没有该数据");
        }
    }

    private int find(Person person) {
        for (int index = 0; index < db.length; index++) {
            if (db[index] != null && db[index].getId() == person.getId()){
                return index;
            }
        }
        return -1;
    }

    @Override
    public void findAll() {
        for (int index = 0; index < db.length; index++) {
            Person person = db[index];
            if (person != null)
                System.out.println(person);
        }

    }
}
